<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <div class="login_form">
          <el-form :model="login_data" ref="login_ref" :rules="login_rules">
            <el-form-item>
              <el-radio-group v-model="childSys" @change="childSys_change">
                <el-radio label="PC_CAR_TEAM" size="large">车队后台管理系统</el-radio>
                <el-radio label="PC_TRAVEL_AGENCY" size="large">旅行社后台管理系统</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item prop="phone">
              <el-input v-model="login_data.phone" type="number" placeholder="请输入手机号" :prefix-icon="Iphone" clearable />
            </el-form-item>
            <el-form-item style="margin-bottom: 5px;" prop="loginPass">
              <el-input v-model="login_data.loginPass" type="password" placeholder="请输入密码" :prefix-icon="Lock" clearable
                show-password /> </el-form-item>
          </el-form>

          <el-button style="margin-top: 20px; width:100%" type="primary" @click="login_btn(login_ref)"
            :icon="User">登录</el-button>

        </div>
      </el-col>
    </el-row>

    <el-dialog v-model="login_dialog_off" title="请选择登录单位" width="500" :show-close="false">
      <el-form :model="login_data" label-width="auto" style="max-width: 600px">
        <el-form-item label="登录单位:">
          <el-select style="width: 220px;" v-model="login_data.companyNum" placeholder="请选择登录单位">
            <el-option v-for="item in getCompanyByPhone_lit" :key="item.companyNum" :label="item.companyName"
              :value="item.companyNum" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="login_dialog_off = false">取消</el-button>
          <el-button type="primary" @click="login_dialog_ok">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { Iphone, Lock, User } from '@element-plus/icons-vue'
import { ElMessage, ElLoading, ElMessageBox } from 'element-plus'
import loginStore from '@/store/login/index.js'
const $loginStore = loginStore()
import loginStore2 from '@/store/main/index.js'
const $loginStore2 = loginStore2()
import { useRouter, useRoute } from 'vue-router'
const $useRouter = useRouter()
const $useRoute = useRoute()
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time.js'
import {
  getCompanyByPhone,getLUserInfo
} from '@/api/login.js'

//#region 登陆
const login_data = reactive({
  phone: '',
  loginPass: '',
  companyNum: '',
  childSys: '',
})
const childSys = ref('')
const childSys_change = () => {
  localStorage.setItem('childSys', childSys.value)
}

const login_dialog_off = ref(false)
const getCompanyByPhone_lit = ref([])
const login_btn = (login_ref) => {
  if (!login_ref) return
  login_ref.validate(async (valid, fields) => {
    if (valid) {
      if (childSys.value) {
        getCompanyByPhone({
          loginPhone: login_data.phone,
        }).then(res => {
          getCompanyByPhone_lit.value = res.data
          login_data.companyNum = res.data[0].companyNum
        })
        localStorage.setItem('childSys', childSys.value)
        login_dialog_off.value = true
      } else {
        return ElMessage({
          message: '请选择系统',
          type: 'warning',
        })
      }




    } else {
      console.log('登录表单验证不通过!', fields)
    }
  })

}
const login_dialog_ok = async () => {

  try {
    await $loginStore.userLogin(login_data)
    $loginStore2.router_list = [{
      meta: {
        title: "信息概览",
        hidden: false, // 路由是否隐藏：true隐藏，false显示
        icon: "Flag",
      },
      path: '/welcome_page',
      name: "welcome_page", // 命名路由
    }]
    $loginStore2.router_name = {}
    login_data.childSys = childSys.value

    getLUserInfo({}).then(res => {
      localStorage.setItem('loginSysRole', res.data.loginSysRole)
      localStorage.setItem('reload', '0')
      $useRouter.push('/personal_center')
      
    })

   

    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `HI,${getTime()}好`,
    })
  } catch (error) {
    ElNotification({
      type: "error",
      message: error.message,
    })
  }




}
const login_ref = ref()

const login_rules = reactive({
  phone: [
    { required: true, message: '请输入手机号', trigger: 'change' },
    { min: 11, max: 11, message: '收输入11位数字的手机号', trigger: 'blur' },
  ],
  loginPass: [
    { required: true, message: '请输入密码', trigger: 'change' },
  ],
})

//#endregion


</script>

<style scoped lang='scss'>
.login_container {
  width: 100%;
  height: 100vh;
  background-image: url('@/assets/images/login-bg.jpg');
  background-size: cover;

  .login_form {
    position: relative;
    width: 45%;
    top: 35vh;
    left: 5vw;
    padding: 40px 40px 20px 40px;
    border-radius: 10px;
    background-color: #fff;
  }
}
</style>
